﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreEx.Database.Query.Expressions;

namespace CoreEx.Database.Linq
{
    /// <summary>
    /// Represents the projection that targets a specific <see cref="TableSource"/> instance.
    /// </summary>
    public class Projection
    {
        private static readonly DbExpressionFactory _expressionFactory = new DbExpressionFactory();
        
        /// <summary>
        /// Initializes a new instance of the <see cref="Projection"/> class.
        /// </summary>
        /// <param name="tableSource">The <see cref="TableSource"/> that 
        /// represents the source for this <see cref="Projection"/></param>.
        public Projection(TableSource tableSource)
        {
            TableSource = tableSource;
            ProjectedColumns = new List<ProjectedColumn>();
        }

        /// <summary>
        /// Gets the <see cref="TableSource"/> instance that represents 
        /// the source for this <see cref="Projection"/>.
        /// </summary>
        public TableSource TableSource { get; private set; }

        /// <summary>
        /// Gets a list of columns projected from the table represented by the <see cref="TableSource"/>. 
        /// </summary>
        public IList<ProjectedColumn> ProjectedColumns { get; private set; }


        public DbExpression DbExpression
        {
            get
            {
                return _expressionFactory
                    .List(ProjectedColumns.Select(pc => pc.DbExpression).ToArray());
            }
        } 

    }
}
